package com.boot.config;

import com.boot.interceptor.LoginInterceptor;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;


@Slf4j
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    @Bean
    SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
        http
                .authorizeHttpRequests(auth -> auth
                        .requestMatchers(
                                "/doc.html",   //Knife4j 的增强文档页面。
                                "/v3/api-docs/**",  //OpenAPI 的 JSON 数据接口。
                                "/swagger-ui/**",   //Swagger UI 静态资源。
                                "/webjars/**"  //WebJars 提供的前端库（如 Swagger UI 的 JS/CSS）
                        ).permitAll()   //允许所有用户（包括未登录用户）访问这些路径。
                        .requestMatchers("/**").permitAll()
                )
                .csrf(AbstractHttpConfigurer::disable);  // 关闭 CSRF（测试用）
        return http.build();
    }

    @Value("${file.upload.dir}")
    private String fileUploadDir;


    //设置静态资源映射
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        log.info("开始设置静态资源映射...");
        registry.addResourceHandler("/doc.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
        registry.addResourceHandler("/upload/**").addResourceLocations("file:" + fileUploadDir);
    }


    @Autowired
    private ObjectMapper objectMapper; // 注入自定义的 ObjectMapper

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        // 移除默认的 Jackson 转换器
        converters.removeIf(c -> c instanceof MappingJackson2HttpMessageConverter);
        // 添加自定义的转换器，使用自定义的 ObjectMapper
        converters.add(new MappingJackson2HttpMessageConverter(objectMapper));
    }

    @Autowired
    private LoginInterceptor loginInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(loginInterceptor).addPathPatterns("/**")
                .excludePathPatterns("/employee/login");
    }
}
